package de.baumann.browser.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import de.baumann.browser.R
import de.baumann.browser.api.net.vo.OcTransfer

class OcTransferAdapter(var context: Context, var rvItemClickListener: RvItemClickListener) : RecyclerView.Adapter<OcTransferAdapter.TransferViewHolder>() {

    var transfers: List<OcTransfer>? = null
        set(value) {
            field = value
            if (value != null) {
                notifyDataSetChanged()
            }
        }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TransferViewHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_tranfer, parent, false)
        return TransferViewHolder(view)
    }

    override fun getItemCount(): Int {
        return if (transfers == null) {
            0
        } else {
            transfers!!.size
        }
    }

    override fun onBindViewHolder(holder: TransferViewHolder, position: Int) {
        holder.tvTransferAddress.text = transfers!![position].toAddress
        holder.tvTransferTime.text = transfers!![position].createTime
        when (transfers!![position].flag) {
            "out" -> {
                holder.tvTransferAmount.text = "-" + transfers!![position].amount
                holder.ivTransferIcon.setImageResource(R.drawable.zc)
                holder.tvTransferAmount.setTextColor(context.resources.getColor(R.color.green_06CC4C))

            }
            "in" -> {
                holder.tvTransferAmount.text = transfers!![position].amount
                holder.ivTransferIcon.setImageResource(R.drawable.zr)
                holder.tvTransferAmount.setTextColor(context.resources.getColor(R.color.blue_3A81F6))
            }
            "fail" -> {
                holder.ivTransferIcon.setImageResource(R.drawable.shibai)
                holder.tvTransferAmount.setTextColor(context.resources.getColor(R.color.red_E02121))
                holder.tvTransferAmount.text = "操作失败"
            }

            "air" -> {
                holder.ivTransferIcon.setImageResource(R.drawable.wakuang_icon)
                holder.tvTransferAmount.setTextColor(context.resources.getColor(R.color.color_FFBE0D))
                holder.tvTransferAmount.text = transfers!![position].amount
            }
        }
    }


    inner class TransferViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var ivTransferIcon: ImageView = itemView.findViewById(R.id.ivTransferIcon)
        var tvTransferAddress: TextView = itemView.findViewById(R.id.tvTransferAddress)
        var tvTransferTime: TextView = itemView.findViewById(R.id.tvTransferTime)
        var tvTransferAmount: TextView = itemView.findViewById(R.id.tvTransferAmount)

        init {
            itemView.setOnClickListener {
                rvItemClickListener.onClick(layoutPosition)
            }
        }
    }
}